package com.springmvc.web.servlet;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * 处理器执行链
 */
public class HandlerExecutionChain {

    /**
     * 处理器方法，实际上底层是HandlerMethod
     */
    private Object handler;

    /**
     * 本次请求要执行的拦截器列表
     */

    private List<HandlerInterceptor> interceptors;


    /**
     * 当前拦截器索引
     */
    private int interceptorIndex = -1;


    public HandlerExecutionChain(Object handler, List<HandlerInterceptor> interceptors) {
        this.handler = handler;
        this.interceptors = interceptors;
    }

    public HandlerExecutionChain() {
    }

    public Object getHandler() {
        return handler;
    }

    public void setHandler(Object handler) {
        this.handler = handler;
    }

    public List<HandlerInterceptor> getInterceptors() {
        return interceptors;
    }

    public void setInterceptors(List<HandlerInterceptor> interceptors) {
        this.interceptors = interceptors;
    }

    public int getInterceptorIndex() {
        return interceptorIndex;
    }

    public void setInterceptorIndex(int interceptorIndex) {
        this.interceptorIndex = interceptorIndex;
    }

    /**
     * 执行拦截器的preHandle方法
     *
     * @param request
     * @param response
     * @return
     */
    public boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) {
        // 遍历拦截器列表
        for (int i = 0; i < interceptors.size(); i++) {
            // 获取拦截器
            HandlerInterceptor interceptor = interceptors.get(i);
            try {
                // 执行拦截器的preHandle方法
                boolean apply = interceptor.preHandle(request, response, handler);
                if (!apply) {
                    // 执行 AfterCompletion
                    triggerAfterCompletion(request, response, null);
                    return false;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 拦截器索引
            interceptorIndex = i;
        }
        return true;
    }

    public void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) {
        // 遍历拦截器列表   要逆序
        for (int i = interceptors.size() - 1; i >= 0; i--) {
            // 获取拦截器
            HandlerInterceptor interceptor = interceptors.get(i);
            try {
                // 执行拦截器的postHandle方法
                interceptor.postHandle(request, response, handler, mv);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Object o) {
            // 遍历拦截器列表   要逆序
        for (int i = interceptorIndex; i >= 0; i--) {
            // 获取拦截器
            HandlerInterceptor interceptor = interceptors.get(i);
            try {
                // 执行拦截器的afterCompletion方法
                interceptor.afterCompletion(request, response, handler, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
